package com.edou.member.controller.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.constant.SessionConstant;
import com.edou.base.controller.BaseController;
import com.edou.base.token.TokenValidate;
import com.edou.member.entity.MemberCollection;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberCollectionForm;
import com.edou.member.service.IMemberCollectionService;
import com.edou.member.service.IMemberUserService;



/**
 * 会员收藏
 * @creator     
 * @create-time 2016/4/18 17:15:31
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberCollectionController")
@Scope("prototype")
@RequestMapping("/member/collection")
public class MemberCollectionController extends BaseController {

	@Resource(name = "memberCollectionServiceImpl")
	private IMemberCollectionService memberCollectionService;
	
	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;
	

	/**
	 * 保存会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/add_collection.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveMemberCollection(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpSession session){
		try {
			MemberUser currentUser = (MemberUser)session.getAttribute(SessionConstant.CURRENT_MEMBER);
			String objectId = model.getObjectId();
			Integer objectType = model.getObjectType();
			String objectName = model.getItem().getObjectName();
			String memberId = currentUser.getId();
			String memberName = currentUser.getName();
			
			// 验证该会员是否收藏过该实体
			MemberCollection collection = memberCollectionService.getMemberCollectionByObjectIdTypeMemberId(objectId, objectType, memberId);
			if(null != collection){
				return ajaxJSON(Status.error, "您已经收藏过了，不用再次收藏。");
			}else{
				collection = new MemberCollection();
				collection.setObjectId(objectId);
				collection.setObjectType(objectType);
				collection.setObjectName(objectName);
				collection.setMemberUserId(memberId);
				collection.setMemberUserName(memberName);
				memberCollectionService.add(collection);
				return ajaxJSON(Status.success,"收藏成功");
			}
			
		} catch (Exception e) {
			String msg = "保存会员收藏时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "收藏失败，请联系管理员");
		}
	}
	
	/**
	 * 取消会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/del_collection.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView delMemberCollection(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpSession session){
		try {
			MemberUser currentUser = (MemberUser)session.getAttribute(SessionConstant.CURRENT_MEMBER);
			String objectId = model.getObjectId();
			Integer objectType = model.getObjectType();
			String memberId = currentUser.getId();
			
			// 验证该会员是否收藏过该实体
			memberCollectionService.delCollection(objectId, objectType, memberId);
			return ajaxJSON(Status.success,"取消收藏成功");
		} catch (Exception e) {
			String msg = "删除会员收藏时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "取消收藏失败，请联系管理员");
		}
	}
	
	/**
	 * 删除会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			memberCollectionService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除会员收藏时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

}